package com.java110.mall.shop.cmd.shopVipUser;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.CmdContextUtils;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.dto.account.AccountDto;
import com.java110.dto.integral.IntegralDto;
import com.java110.dto.vipUser.ShopUserAccountVipDto;
import com.java110.dto.vipUser.ShopVipUserDto;
import com.java110.dto.user.UserDto;
import com.java110.intf.acct.IAccountInnerServiceSMO;
import com.java110.intf.acct.IIntegralSettingV1InnerServiceSMO;
import com.java110.intf.acct.IIntegralV1InnerServiceSMO;
import com.java110.intf.shop.IShopVipUserInnerServiceSMO;
import com.java110.intf.user.IUserV1InnerServiceSMO;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.ListUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Java110Cmd(serviceCode = "shopVipUser.queryShopUserAccountAndVip")
public class QueryShopUserAccountAndVipCmd extends Cmd {

    @Autowired
    private IUserV1InnerServiceSMO userV1InnerServiceSMOImpl;


    @Autowired
    private IAccountInnerServiceSMO accountInnerServiceSMOImpl;

    @Autowired
    private IShopVipUserInnerServiceSMO shopVipUserInnerServiceSMOImpl;

    @Autowired
    private IIntegralV1InnerServiceSMO integralV1InnerServiceSMOImpl;

    @Autowired
    private IIntegralSettingV1InnerServiceSMO integralSettingV1InnerServiceSMOImpl;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        Assert.hasKeyAndValue(reqJson, "shopId", "未包含店铺ID");
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {

        String userId = CmdContextUtils.getUserId(context);

        ShopUserAccountVipDto shopUserAccountVipDto = new ShopUserAccountVipDto();
        shopUserAccountVipDto.setIntegral("0");
        shopUserAccountVipDto.setIntegralMoney("0");
        shopUserAccountVipDto.setCash("0");
        shopUserAccountVipDto.setVipLevel("0");
        shopUserAccountVipDto.setVipLevelName("0级");

        UserDto userDto = new UserDto();
        userDto.setUserId(userId);

        List<UserDto> userDtos = userV1InnerServiceSMOImpl.queryUsers(userDto);

        Assert.listOnlyOne(userDtos, "用户 不存在");


        //查询会员
        ShopVipUserDto shopVipUserDto = new ShopVipUserDto();
        shopVipUserDto.setVipTel(userDtos.get(0).getTel());
        shopVipUserDto.setShopId(shopUserAccountVipDto.getShopId());
        List<ShopVipUserDto> shopVipUserDtos = shopVipUserInnerServiceSMOImpl.queryShopVipUsers(shopVipUserDto);

        if (!ListUtil.isNull(shopVipUserDtos)) {
            shopUserAccountVipDto.setVipLevel(shopVipUserDtos.get(0).getVipLevel());
            shopUserAccountVipDto.setVipLevelName(shopVipUserDtos.get(0).getLevelName());
        }

        IntegralDto integralDto = new IntegralDto();
        integralDto.setLink(userDtos.get(0).getTel());
        integralDto.setObjType(IntegralDto.OBJ_TYPE_PERSON);
        List<IntegralDto> integralDtos = integralV1InnerServiceSMOImpl.queryIntegrals(integralDto);
        if (!ListUtil.isNull(integralDtos)) {
            shopUserAccountVipDto.setIntegral(integralDtos.get(0).getAmount());
            double money = integralSettingV1InnerServiceSMOImpl.computeMoney(Integer.parseInt(integralDtos.get(0).getAmount()));
            shopUserAccountVipDto.setIntegralMoney(money + "");
        }

        AccountDto accountDto = new AccountDto();
        accountDto.setLink(userDtos.get(0).getTel());
        accountDto.setAcctType(AccountDto.ACCT_TYPE_CASH);
        accountDto.setObjType(AccountDto.OBJ_TYPE_PERSON);
        List<AccountDto> accountDtos = accountInnerServiceSMOImpl.queryAccounts(accountDto);
        if (!ListUtil.isNull(accountDtos)) {
            shopUserAccountVipDto.setCash(accountDtos.get(0).getAmount());
        }


        context.setResponseEntity(ResultVo.createResponseEntity(shopUserAccountVipDto));

    }
}
